//
//  ALVisitorView.swift
//  WeiBo-Swift
//
//  Created by Miaoyang on 16/4/8.
//  Copyright © 2016年 Miaoyang. All rights reserved.
//

import UIKit

//1.定义一个代理
protocol ALVisitorViewDeleget:NSObjectProtocol{
    //2.定义代理方法
    func visitorViewDidClickRegisterButton()
    func visitorViewDidClickLoginButton()
}

class ALVisitorView: UIView {
    
    
    //3.定义代理属性
    weak var delegate :ALVisitorViewDeleget?
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override  init(frame: CGRect) {
        super.init(frame: frame)
        //添加子控件及设置frame
        updataUI()
        //设置背景颜色
        backgroundColor = UIColor(white: 237 / 255.0, alpha: 1)
        
    }
    //核心动画
    func startRotation() {
        //使用核心动画
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        //设置旋转幅度
        anim.toValue = M_PI * 2
        //设置旋转次数
        anim.repeatCount = MAXFLOAT
        //旋转持续时间
        anim.duration = 20
        //动画完成不要移除动画
        anim.removedOnCompletion = false
        //添加到图层
        iconView.layer.addAnimation(anim, forKey: nil)
    }
    //设置访客界面的样式
    func updataVisitorInfor(imageName:String,message:String){
        
        //设置图片
        iconView.image = UIImage(named: imageName)
        
        //设置文字
        messageLabel.text = message
        //隐藏房子和遮罩
        homeView.hidden = true
        
        coverVier.hidden = true
        
    }
    
    private func updataUI(){
        //添加子控件
        addSubview(iconView)
        addSubview(coverVier)
        addSubview(homeView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        //禁用自动布局
        iconView.translatesAutoresizingMaskIntoConstraints = false
        coverVier.translatesAutoresizingMaskIntoConstraints = false
        homeView.translatesAutoresizingMaskIntoConstraints = false
        messageLabel.translatesAutoresizingMaskIntoConstraints = false
        registerButton.translatesAutoresizingMaskIntoConstraints = false
        loginButton.translatesAutoresizingMaskIntoConstraints = false
        
        
        
        //转轮的X
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant:0 ))
        //转轮的Y
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: -40))
        /// 遮盖
        addConstraint(NSLayoutConstraint(item: coverVier, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: coverVier, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: coverVier, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: coverVier, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: registerButton, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
        
        //房子X
        addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        //房子Y
        addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        
        //Label的X
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        //Label的TOP
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        //label的宽度
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 250))
        
        //注册按钮顶部
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 28))
        //注册按钮的左边
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
        //注册按钮的宽度
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        //注册按钮的高度
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        //登录按钮顶部
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: registerButton, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        //登录按钮右边
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0))
        //登录按钮宽度
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: registerButton, attribute: NSLayoutAttribute.Width, multiplier: 1, constant: 0))
        //登录按钮高度
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: registerButton, attribute: NSLayoutAttribute.Height, multiplier: 1, constant: 0))
    }
    //MARK:添加点击事件
    
    @objc private func didClickRegisterButton() {
        //4.调用代理方法
        delegate?.visitorViewDidClickRegisterButton()
        
    }
    @objc private func didClickLoginButton() {
        //4.调用代理方法
        delegate?.visitorViewDidClickLoginButton()
        
    }
    //MARK:懒加载子控件
    //转轮
    private lazy var iconView:UIImageView = UIImageView(image: UIImage(named:"visitordiscover_feed_image_smallicon"))
    //遮罩
    private lazy var coverVier:UIImageView = UIImageView(image:UIImage(named:"visitordiscover_feed_mask_smallicon"))
    //房子
    private lazy var homeView:UIImageView = UIImageView(image: UIImage(named:"visitordiscover_feed_image_house"))
    //label,这里要使用闭包,因为此处要设置一些属性值
    
    private lazy var messageLabel:UILabel = {
        let label = UILabel()
        //设置内容,字体大小,换行,对其方式
        label.text = "关注一些人,看看什么情况"
        label.font = UIFont.systemFontOfSize(18)
        label.numberOfLines = 0
        label.textAlignment = NSTextAlignment.Center
        
        return label
        
    }()
    
    //登录按钮
    private lazy var loginButton:UIButton = {
        
        let button = UIButton()
        //设置内容
        button.setTitle("登录", forState: UIControlState.Normal)
        
        //设置背景图
        button.setBackgroundImage(UIImage(named:"common_button_white_disable"),forState: UIControlState.Normal)
        
        button.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        //添加点击事件
        button.addTarget(self, action: #selector(ALVisitorView.didClickLoginButton), forControlEvents: UIControlEvents.TouchUpInside)


        
        return button
        
    }()
    
    //注册按钮
    
    private lazy var registerButton:UIButton = {
        
        let button = UIButton()
        
        button.setTitle("注册", forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named:"common_button_white_disable"), forState: UIControlState.Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        
        //添加点击事件
        button.addTarget(self, action: #selector(ALVisitorView.didClickRegisterButton), forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
        
    }()
    
    
    
    
    
}





